package xyz.bali16.module.article.aware;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import xyz.bali16.module.article.entity.Article;
import xyz.bali16.module.article.entity.Category;
import xyz.bali16.module.article.entity.Collect;
import xyz.bali16.module.article.entity.Like;
import xyz.bali16.module.article.model.collection.CollectionPageParam;
import xyz.bali16.module.article.model.like.LikePageParam;
import xyz.bali16.module.article.service.ArticleRankServiceCaller;
import xyz.bali16.module.article.service.ArticleService;
import xyz.bali16.module.article.service.CollectionService;
import xyz.bali16.module.article.service.LikeService;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;

/**
 * 启动时候负责加载最近七天的帖子点赞、评论、收藏、浏览进redis
 */
@Component
public class ContextStartup implements ApplicationRunner {


    @Autowired
    ArticleRankServiceCaller articleRankServiceCaller;

    @Autowired
    ArticleService articleService;
    @Autowired
    LikeService likeService;
    @Autowired
    CollectionService collectionService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化周热榜

        // 获取7天的发表的文章
        List<Article> articles = articleService.list(new QueryWrapper<Article>()
                .ge("gmt_created", DateUtil.offsetDay(new Date(), -7)) // 11号
                .select("id, title, user_id, comment_count, views, gmt_created")
        );

        for(Article article:articles){
            LikePageParam likePageParam = new LikePageParam();
            likePageParam.setTargetId(article.getId());
            likePageParam.setCurrent(0);
            likePageParam.setSize(10);
            Integer likeNum = (int)likeService.getPage(likePageParam).getTotal();

            CollectionPageParam collectionPageParam = new CollectionPageParam();
            collectionPageParam.setTargetId(article.getId());
            collectionPageParam.setCurrent(0);
            collectionPageParam.setSize(10);
            Integer collectNum = (int) collectionService.getPage(collectionPageParam).getTotal();
            articleRankServiceCaller.initWeekRank("commentCount",article.getId(),article.getTitle(),article.getCommentCount(),article.getViews(),article.getGmtCreated(),likeNum,collectNum);
            articleRankServiceCaller.initWeekRank("views",article.getId(),article.getTitle(),article.getCommentCount(),article.getViews(),article.getGmtCreated(),likeNum,collectNum);
            articleRankServiceCaller.initWeekRank("like",article.getId(),article.getTitle(),article.getCommentCount(),article.getViews(),article.getGmtCreated(),likeNum,collectNum);
            articleRankServiceCaller.initWeekRank("collection",article.getId(),article.getTitle(),article.getCommentCount(),article.getViews(),article.getGmtCreated(),likeNum,collectNum);
        }

        articleRankServiceCaller.zunionAndStoreLast7DayForWeekRank("commentCount");
        articleRankServiceCaller.zunionAndStoreLast7DayForWeekRank("views");
        articleRankServiceCaller.zunionAndStoreLast7DayForWeekRank("like");
        articleRankServiceCaller.zunionAndStoreLast7DayForWeekRank("collection");
    }

}
